package com.king.hrmsdev.service;


import com.king.hrmsdev.entity.Salary;
import com.king.hrmsdev.mapper.SalaryMapper;
import com.king.hrmsdev.pojo.VEmployReword;
import com.king.hrmsdev.pojo.VSalaryinfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.List;

/**
 * @Auther:SnakeKing
 * @Date: 2019-09-22
 * @Description: com.king.hrmsdev.service
 * @version:1.0
 */
@Service
public class SalaryService {

    @Resource
    private SalaryMapper salaryMapper;


    public List<VSalaryinfo> QuaryAllSalary() {
        List<VSalaryinfo> lists = salaryMapper.QuarySalary(0, null, null);
        for (VSalaryinfo Vsalaryinfo : lists) {
            Integer job_id = Vsalaryinfo.getJob_id();
            Date thisday = Vsalaryinfo.getThismonth();
            Vsalaryinfo.setCheckMoney(BigDecimal.valueOf(check_moeny(job_id, thisday)));
        }
        return lists;
    }


    public List<VSalaryinfo> QuarySalary(int job_id, String ename, Date date) {
        List<VSalaryinfo> lists = salaryMapper.QuarySalary(job_id, ename, date);
        for (VSalaryinfo Vsalaryinfo : lists) {
            Integer id = Vsalaryinfo.getJob_id();
            Date thisday = Vsalaryinfo.getThismonth();
            Vsalaryinfo.setCheckMoney(BigDecimal.valueOf(check_moeny(id, thisday)));
        }
        System.out.println(lists.toString());
        return lists;
    }

    //计算员工考勤奖金
    public int check_moeny(int job_id, Date checktime) {
        VEmployReword thisman = salaryMapper.QuaryRewordByID(job_id, checktime);
        int overtime = thisman.getOvertime();//统计当月加班时间
        int late = thisman.getLate();//统计迟到次数
        int leaveearly = thisman.getLeaveearly();//统计早退次数
        int leave = thisman.getAleave();//统计请假次数
        int absenteeism = thisman.getAbsenteeism();//统计旷工次数
        int money = overtime * 22 - late * 10 - leaveearly * 20 - leave * 50 - absenteeism * 100;
        return money;
    }


    //更新员工薪水纪录
    public void AddSalary() {
        salaryMapper.deleteall();
        salaryMapper.clearid();
        List<VSalaryinfo> salarylists = salaryMapper.QuaryAllSalary();
        for (VSalaryinfo onesalary : salarylists) {
            Integer job_id = onesalary.getJob_id();
            Date thisday = onesalary.getThismonth();
            onesalary.setCheckMoney(BigDecimal.valueOf(check_moeny(job_id, thisday)));
            onesalary.setFinalMoney(onesalary.getBaseMoney().add(onesalary.getCheckMoney()));
        }
        for (int i = 0; i < salarylists.size(); i++) {
            Salary thissalary = new Salary();
            thissalary.setJob_id(salarylists.get(i).getJob_id());
            thissalary.setPayday(salarylists.get(i).getThismonth());
            thissalary.setFinasalary(salarylists.get(i).getFinalMoney());
            salaryMapper.AddSalary(thissalary);
        }
    }

}

